package com.javatest;

import java.util.Random;

/**
 * @author LL
 * 找出数组中出现次数最多的数字
 * @create 2020-12-11 10:20
 */
public class Subject18 {

    public static void selectCount(int[] array){
        int tmp = 1;
        int index = -1;
        int s = 0;
        int[] max = new int[array.length];
        int x = 0;
        for (int i = 0; i < array.length; i++) {
            int count = 0;
            for (int j = 0; j < array.length; j++) {
                if (array[i] == array[j]){
                    count++;
                    s = array[i];
                }
            }
            if (count > tmp){
                tmp = count;
                index = i;
                max = new int[array.length];
                x = 0;
            }else if (count == tmp){
                max[x++] = count;
            }
        }
        max[x+1] = tmp;
        for (int i = 0; i < max.length; i++) {
            for (int j = 0; j < max.length; j++) {
                if (max[j] > max[j+1]) {
                    int st = max[j];
                    max[j] = max[j+1];
                    max[j] = st;
                }
            }
        }
        for (int i = 0; i < max.length; i++) {
            System.out.println(max[i]);
        }
    }

    public static void moreThanHaft1(int[] num) {
        int temp = 1;
        int index = -1;
        for (int i = 0; i < num.length; i++) {
            int count = 0;
            for (int value : num) {
                if (num[i] == value) {
                    count++;
                }
            }
            if (count > temp) {
                temp = count;
                index = i;
            }
        }
       //index == -1 ? -1 : num[index];
        System.out.println("元素个数："+temp+"元素为："+(index == -1 ? -1 : num[index]));
    }

    public static void main(String[] args) {
        int s = 10;
        int[] array = new int[s];
        for (int i = 0; i < s; i++) {
            array[i] = (int)Math.round(Math.random()*10);
        }
        int[] nints = {5,5,5,1,0,2,4,4,4,6,6,6,6,6};
        selectCount(nints);
//        int[] num = { 1, 2, 2, 2, 4, 4, 4, 6, 4, 6, 8, 2, 7, 7 };
//        moreThanHaft1(num);
//        int[] nints = {5,5,1,0,2,4,4,4};
//        int i = moreThanHaft1(nints);
//        System.out.println("i:"+1);
    }
}
